package org.example.soft.service.impl;

import org.example.soft.Entity.AdminDTO;
import org.example.soft.Entity.UserDTO;
import org.example.soft.common.Result;
import org.example.soft.mapper.AdminDTOmapper;
import org.example.soft.mapper.UserDTOmapper;
import org.example.soft.pojo.Admin;
import org.example.soft.service.AdminDTOservice;

import com.github.yulichang.base.MPJBaseServiceImpl;
import jakarta.annotation.Resource;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
@AllArgsConstructor
public class AdminDTOserviceImpl extends MPJBaseServiceImpl<AdminDTOmapper, AdminDTO> implements AdminDTOservice {
    @Resource
    private UserDTOmapper userMapper;
    private AdminDTOmapper adminMapper;

    @Override
    public Long Register(AdminDTO admin) {
        return this.adminMapper.insertadmin(admin);
    }
    @Override
    public Long Register(UserDTO user){
        return this.adminMapper.insertuser(user);
    }
    @Override
    public String Login(String name,String password){
        System.out.println(name+password);
        AdminDTO admin = adminMapper.getAdminByName(name);
        if (admin != null) {
            // Check if the provided password matches the stored password (assuming password hashing)
            if (Objects.equals(password, admin.getPassword())) {
                return "Login success"; // Return a success message or token
            }
        }
        return "Login failed"; // Return a failure message
    }

    public List<UserDTO> SelectallUser() {
        return this.userMapper.GetAllUser();
    }

    @Override
    public UserDTO SelectById(Long id) {
        return this.userMapper.selectById(id);
    }

    @Override
    public Result<Object> DeletedById(Long id) {
        if(this.userMapper.DeleteUserById(id))
        {
            return Result.success();
        }
        else return Result.fail();
    }

    @Override
    public Result<Object> UpdateById(UserDTO userDTO) {
        if(this.userMapper.UpdateUserById(userDTO))
            return Result.success();
        else return Result.fail();
    }
}
